/* ================================================================================ *//**
 \license     %LICENSE%
 \file        rtos_tasks.c
 \author      ukasz Krzak
 \purpose     STM32 RTOS demo application
 \compiler    %COMPILER%
 \hardware    independent
 \doc         doxygen
 \brief       RTOS task definitions
 \comments
*//* ================================================================================= */

#include "stm32f10x.h"
//#include "stm32f10x_conf.h"
#include <stdio.h>
#include <string.h>

#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
#include "timers.h"

#include "lwip/sys.h"
#include "lwip/stats.h"

#include "FreeRTOS_lwIP/netif/enc28j60.h"

// -----------------------------------------------------------------------------
//  TASK_LED1
//
//  This task simply blinks a LED
// -----------------------------------------------------------------------------
void TASK_LED1( void* pvParameters )
{

    // dummy instruction to avoid compiler warnings
    (void) pvParameters;


	GPIO_InitTypeDef GPIO_InitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	/**
	 *	LED1 -> PB0   LED2 -> PB1
	 */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_Init(GPIOB, &GPIO_InitStructure);

    while (1) {
        GPIO_SetBits(GPIOB, GPIO_Pin_0);
        vTaskDelay(1000);
        GPIO_ResetBits(GPIOB, GPIO_Pin_0);
        vTaskDelay(1000);

//    	if(enc28j60linkup())
//    	{
//    		printf("LINK: UP\n\r");
//    	}
//    	else
//    	{
//    		printf("LINK: DOWN\n\r");
//    	}
    }
} /* TASK_LED1 */


// -----------------------------------------------------------------------------
//  TASK_USART
//
//  This task simply blinks a LED
// -----------------------------------------------------------------------------
void TASK_USART(void* pvParameters)
{
    extern xSemaphoreHandle sem;

    while (1) {
        if (NULL != sem) {
            if (pdTRUE == xSemaphoreTake(sem, 1000)) {
                printf("Hello World\r\n");
                xSemaphoreGive(sem);
            }
        }

        vTaskDelay(100);
    }

} /* TASK_USART */


extern xTaskHandle xEthIntpuTskHandle;
extern xTaskHandle xLEDTskHandle;
extern sys_thread_t xLWIPTskHandler;
//extern xTaskHandle xStatTskHandle;


// -----------------------------------------------------------------------------
//  TASK_STATISTICS
//
//  This task gets OS run time statistics and prints them on the serial port
// -----------------------------------------------------------------------------
void TASK_STATISTICS( void *pvParameters )
{
	char buf[120];
	unsigned portBASE_TYPE stats, led, eth_inp, lwip;

	while (1) {

		strcpy(buf, "\r\n--------------------\r\n");
		_write(0, buf, strlen(buf));

		sprintf(buf, "SysTicks: %d  Tim: %d", (int)xTaskGetTickCount(), get_tim());
		_write(0, buf, strlen(buf));

		// get FreeRTOS task statistics
		vTaskGetRunTimeStats((signed char*)buf);
		// print them in the console
		_write(0, buf, strlen(buf));

		stats = uxTaskGetStackHighWaterMark(NULL);
		eth_inp = uxTaskGetStackHighWaterMark(xEthIntpuTskHandle);
		led = uxTaskGetStackHighWaterMark(xLEDTskHandle);
		lwip = uxTaskGetStackHighWaterMark(xLWIPTskHandler);

		printf("\r\n--------Tasks stack watermark-------\r\n");
		printf("Stats:		%d\r\n", stats);
		printf("LED:		%d\r\n", led);
		printf("ETH:		%d\r\n", eth_inp);
		printf("lwIP:		%d\r\n", lwip);
		printf("Heap free space:	%d\n\r", xPortGetFreeHeapSize());
		printf("\r\n------------------------------------\r\n");

//		stats_display();

		printf("\r\n--------lwIP MEM stats-------\r\n");
		printf("Avail:		%d\r\n",lwip_stats.mem.avail);
		printf("Max:		%d\r\n",lwip_stats.mem.max);
		printf("Used:		%d\r\n",lwip_stats.mem.used);
		printf("Illegal:	%d\r\n",lwip_stats.mem.illegal);
		printf("Err:		%d\r\n",lwip_stats.mem.err);
		printf("\r\n------------------------------------\r\n");


		// wait 10s
		vTaskDelay(10000);
	}

} /* TASK_STATISTICS */
